package com.projet.xh;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class SortBom {
    /**
     * 排序
     *
     * @param detailList
     * @return
     */
    public static List<PrdBomVO> sortBom(List<PrdBomVO> detailList) {
        if (detailList == null) {
            return new ArrayList<>();
        }
        Collections.sort(detailList, new Comparator<PrdBomVO>() {
            @Override
            public int compare(PrdBomVO o1, PrdBomVO o2) {
                Integer sortNum1 = o1.getSortNum() == null ? 100000 : o1.getSortNum();
                Integer sortNum2 = o2.getSortNum() == null ? 100000 : o2.getSortNum();
                if (sortNum1.equals(sortNum2)) {
                    return o1.getMaterialNo().compareTo(o2.getMaterialNo());
                }
                return sortNum1.compareTo(sortNum2);
            }
        });

        int maxSortNum = 0;
        for (PrdBomVO prdBomVO : detailList) {
            if (prdBomVO.getSortNum() != null) {
                if (maxSortNum < prdBomVO.getSortNum()) {
                    maxSortNum = prdBomVO.getSortNum();
                }
            }
        }
        maxSortNum++;
        for (PrdBomVO prdBomVO : detailList) {
            if (prdBomVO.getSortNum() == null) {
                prdBomVO.setSortNum(maxSortNum++);
            }
        }
        return detailList;
    }
}
